package com.guigu.tms.security.util;

import cn.hutool.core.util.RandomUtil;
import com.guigu.tms.commons.security.entity.AuthUser;
import com.guigu.tms.security.service.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;

@Data
public class ValidateCode {
    @Autowired
    SendService sendService;
    @Autowired
    IAuthUserService authUserService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    private String key;

    public boolean getValidateCode(String phone) {
        //生成验证码
        String code = RandomUtil.randomNumbers(6);
        //发送验证码
        String msg="【短信宝】您的验证码是 "+code+",30秒内有效";
        sendService.sendSms("wkj333","77ca3217e5694aa98b230b81a4e03c85",phone,msg);
        //将验证码保存在redis
        redisTemplate.opsForValue().set(key+phone,code, Duration.ofMinutes(5));
        return true;
    }
}
